import multiprocessing
import os
import subprocess
import threading
import uvicorn
from celerywork.mycelery import celery_app
from fastapi import FastAPI
from exit import init_config
BASE_DIR = str(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

app = FastAPI()
# 这里方法集中处理了,将flask返回出来
init_config(app)
# app.mount("/test",StaticFiles(directory=os.path.join(BASE_DIR, 'test')),name="test")


@app.get("/")
async def root():
    return {"message": "Hello World"}
# # 挂载 Vue 应用的入口文件
# @app.get("/{catch_all:path}")
# async def serve_frontend(catch_all: str):
#
#     return FileResponse("Admin/static/dist/index.html")
#     app.mount("/assets", StaticFiles(directory=os.path.join(BASE_DIR, 'Admin/static/dist/assets/')), name="assets")
#
# @app.get("/hello/{name}")
# async def say_hello(name: str):
#     return {"message": f"Hello {name}"}
def start_celery():
    celery_app.worker_main(argv=['worker', '--loglevel=info',"--pool=threads",'-c',2])
def start_fastapi():
    # subprocess.Popen(['gunicorn', '-w', '4', '-k', "uvicorn.workers.UvicornWorker" ,"- b", "127.0.0.1: 8001"])
    uvicorn.run(app, host="127.0.0.1", port=8001,  reload=False)
def start_flower():
    # 启动 Flower
    # subprocess.Popen是以命令形式执行。
    subprocess.Popen(['celery', '-A', 'celerywork.mycelery', 'flower', '--port=8000'])

worker_thread = threading.Thread(target=start_celery)
flower_process = multiprocessing.Process(target=start_flower)
fastapi_process= multiprocessing.Process(target=start_fastapi)

# celery_process = multiprocessing.Process(target=start_celery)
if __name__ == "__main__":
    fastapi_process.start()

    # flower_process.start()
    worker_thread.start()
    fastapi_process.join()

    worker_thread.join()
    # flower_process.join()










